package com.demo.controller;

import com.demo.entity.Role;
import com.demo.model.JsonMessage;
import com.demo.security.AuthUserDetail;
import com.demo.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("list")
    @ResponseBody
    public JsonMessage list(Authentication authentication, @RequestParam(defaultValue = "0") int pageIndex,
                            @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        PageInfo<Role> page = roleService.list(pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    @PostMapping("path")
    @ResponseBody
    public JsonMessage path(Authentication authentication) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        String roleId = detail.getRoleId();
        List<String> list = roleService.path(roleId);
        message.setData(list);
        return message;
    }
}
